﻿#include "formrealtimedata.h"
#include "ui_formrealtimedata.h"
#include "datasimucache.h"
#include "globalsymbol.h"

#include <QDebug>
#include <QDir>
#include <QStorageInfo>
#include <QTimer>
#include <QRandomGenerator>
#include <QProgressDialog>
#include <QMessageBox>

FormRealtimeData::FormRealtimeData(JsonConfig* jsonConfig, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormRealtimeData),
    m_jsonConfig(jsonConfig),
    m_inFixedChannel(false)
{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

FormRealtimeData::~FormRealtimeData()
{
    delete ui;
}

void FormRealtimeData::changeShowOperation(bool enabled)
{
    ui->chkPlot->setEnabled(enabled);
    ui->chkData->setEnabled(enabled);
    ui->chkTesting->setEnabled(enabled);
}

void FormRealtimeData::clear()
{
    QList<QLabel*> lblDataList = ui->gbxRealtimeData->findChildren<QLabel*>();
    for (int i = 0; i < lblDataList.size(); i++)
        lblDataList.at(i)->setText("0");
}

void FormRealtimeData::initUI()
{
    connect(ui->btnFixedChannel, &QPushButton::clicked, this, &FormRealtimeData::onFixedChannel);
    connect(ui->btnDiskCapacity, &QPushButton::clicked, this, &FormRealtimeData::onDiskCapacity);
    connect(ui->chkPlot, &QCheckBox::stateChanged, this, &FormRealtimeData::onPlotChanged);
    connect(ui->chkTesting, &QCheckBox::stateChanged, this, &FormRealtimeData::onTestingChanged);
}

void FormRealtimeData::init()
{
    m_fixedChannelList = m_jsonConfig->fixedChannelList();
}

void FormRealtimeData::onFixedChannel()
{
    // 固定通道ID列表
    QStringList idList;
    m_inFixedChannel = !m_inFixedChannel;
    if (m_inFixedChannel) {
        for (int i= 0; i < m_fixedChannelList.size(); i++)
            idList << m_fixedChannelList.at(i).id;

        ui->btnFixedChannel->setText(QStringLiteral("删除固定通道"));
    } else {
        // 显示7.5s的进度条
//        QProgressDialog progressDialog;
//        progressDialog.setWindowModality(Qt::WindowModal);
//        progressDialog.setLabelText(QStringLiteral("正在删除固定通道..."));
//        progressDialog.setRange(0, 100);
//        for(int i = 0; i < 10; i++) {
//            if(progressDialog.wasCanceled())
//                break;
//            progressDialog.setValue(QRandomGenerator::global()->bounded(i * 10, (i + 1) * 10));

//            // 延时
//            QEventLoop eventloop;
//            QTimer::singleShot(750, &eventloop, &QEventLoop::quit);
//            eventloop.exec();
//        }
//        progressDialog.setValue(100);

        ui->btnFixedChannel->setText(QStringLiteral("选择固定通道"));
    }

    // 是否勾选固定通道
    for (int i = 0; i < CHANNEL_COUNT; i++) {
        QString id = QString("%1").arg(i, 3, 10, QLatin1Char('0'));
        QCheckBox* chk = ui->gbxRealtimeData->findChild<QCheckBox*>(QString("chkD%1").arg(id));
        if (idList.contains(id))
            chk->setChecked(m_inFixedChannel);
        else
            chk->setChecked(false);

    }

    // 添加要显示的曲线, 如果是取消勾选, 则清空曲线列表
    if (ui->chkPlot->isChecked())
        emit sigUpdatePlot(idList);
}

void FormRealtimeData::onDiskCapacity()
{
    // toNativeSeparator: windows上, 会把路径里的斜杠, 全部转为反斜杠
    QStringList pathList = QDir::toNativeSeparators(qApp->applicationDirPath()).split(QDir::separator());
    if (pathList.empty()) {
        QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("硬盘路径获取错误!"));
        return;
    }

    // 盘符
    QString disk = pathList.first();

    // 磁盘空间
    QStorageInfo si;
    si.setPath(disk);
    si.refresh();

    qint64 totalGB = si.bytesTotal() /1024 / 1024 / 1024;
    qint64 freeGB = si.bytesFree() / 1024 / 1024 / 1024 ;

    QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("硬盘%1 总空间: %2GB, 剩余空间: %3GB").arg(disk).arg(totalGB).arg(freeGB));
}

void FormRealtimeData::onPlotChanged(int)
{
    // 通道ID列表
    QStringList idList;

    if (ui->chkPlot->isChecked()) {
        QList<QCheckBox*> chkDataList = ui->gbxRealtimeData->findChildren<QCheckBox*>();
        for (int i = 0; i < chkDataList.size(); i++) {
            if (chkDataList.at(i)->isChecked()) {
                idList << chkDataList.at(i)->objectName().right(3);
            }
        }
    }

    // 添加要显示的曲线, 如果是取消勾选, 则清空曲线列表
    emit sigUpdatePlot(idList);
}

void FormRealtimeData::onTestingChanged(int)
{
    emit sigTestingVisible(ui->chkTesting->isChecked());
}

void FormRealtimeData::onRefreshData()
{
    for (int i = 0; i < CHANNEL_COUNT; i++) {
        QString id = QString("%1").arg(i, 3, 10, QLatin1Char('0'));
        QCheckBox* chk = ui->gbxRealtimeData->findChild<QCheckBox*>(QString("chkD%1").arg(id));
        QLabel* lbl = ui->gbxRealtimeData->findChild<QLabel*>(QString("lblD%1").arg(id));
        // 已经勾选, 且数据缓存
        if (DataSimuCache::contains(id)) {
            if (chk->isChecked() || ui->chkData->isChecked()) {
                lbl->setText(DataSimuCache::getSimuValue(id));
            }
        }
    }
}
